📑 Table of Contents
In software development, version control is one of the most essential skills. Git is the de facto standard, and GitHub is the collaboration platform built on Git. This article provides a complete guide to mastering Git and GitHub from scratch.
1. What is Git? What is GitHub?
- Git: A distributed version control system that records and manages file change history
- GitHub: A Git repository hosting service with team collaboration features
2. Initial Setup
# Verify Git installation
git --version
# Set your username and email
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# Set default branch name to main
git config --global init.defaultBranch main
# Set editor (VS Code)
git config --global core.editor "code --wait"
# Verify settings
git config --list
3. Essential Commands
# Initialize a repository
git init
# Check status
git status
# Stage files
git add index.html # Specific file
git add . # All changes
# Commit (record changes)
git commit -m "feat: add header component"
# View commit history
git log --oneline --graph -10
# Add remote repository
git remote add origin https://github.com/username/repo.git
# Push (send to remote)
git push -u origin main
# Pull (fetch & merge from remote)
git pull origin main
# Stash changes temporarily
git stash
git stash pop
We recommend the Conventional Commits format: feat: (new feature), fix: (bug fix), docs: (documentation), style: (formatting), refactor: (refactoring)
4. Branch Strategies
# Create and switch to a branch
git checkout -b feature/user-auth
# List all branches
git branch -a
# Switch branches
git checkout main
# Merge a branch
git checkout main
git merge feature/user-auth
# Delete merged branch
git branch -d feature/user-auth
Git Flow (Recommended Branch Strategy)
- main: Mirrors production, always deployable
- develop: Development branch, integration for next release
- feature/*: New feature development (branched from develop)
- hotfix/*: Emergency fixes (branched from main)
- release/*: Release preparation
5. Pull Requests
Pull Requests (PRs) are code review mechanisms. Instead of pushing directly to main, teams use PRs for review to maintain code quality.
Writing Great PRs
- Clear title: "Add user authentication feature"
- Description: What changed and why
- Screenshots: Attach for UI changes
- Keep it small: One feature per PR
- Include tests
6. Resolving Conflicts
Conflicts occur when multiple people edit the same file.
# Conflict markers
<<<<<<< HEAD
const greeting = "Good morning";
=======
const greeting = "Hello";
>>>>>>> feature/greeting-update
# Resolution steps:
# 1. Choose one version or combine both
# 2. Remove conflict markers (<<<, ===, >>>)
# 3. Save the file
const greeting = "Hello"; # Keep the correct version
# 4. Stage and commit
git add .
git commit -m "fix: resolve greeting conflict"
VS Code has a built-in conflict resolution UI with "Accept Current," "Accept Incoming," and "Accept Both" buttons for one-click resolution.
Git/GitHub may seem complex at first, but 70% of daily work uses just 5-6 commands. Start by mastering the add → commit → push → pull cycle, then gradually move on to branches and PRs.